/**
 * @author LiuXu
 * @create 2020-07-03 20:28
 */
package com.cy.pj.goods.controller;

import com.cy.pj.goods.pojo.Goods;
import com.cy.pj.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/goods/")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("doSaveObject")
    public String doSaveObject(Goods entity) {
        goodsService.saveObject(entity);
        return "redirect:doGoodsUI";
    }

    @RequestMapping("doDeleteById")
    public String doDeleteById(Integer id, Model model) {
        //调用业务层对象执行删除操作
        goodsService.deleteById(id);
        return "redirect:doGoodsUI";

        //List<Goods> list = goodsService.findObjects();
        //将数据存储到作用域对象
        //model.addAttribute("list",list);
        //将页面响应到客户端
        //return "goods";//view name
    }
    //请求地址
    //http://localhost:8080/goods/doGoodsUI
    //此访问路径会传递到DispatcherServlet对象
    //DispatcherServlet对象基于用户输入的url找到对应的controller及方法

    @RequestMapping("doGoodsUI")
    public String doGoodsUI(Model model) {
        //获取业务
        List<Goods> list = goodsService.findObjects();
        model.addAttribute("list", list);
        return "goods";//view name
        //此view返回给前端控制器(DispatcherServlet)
        //前端控制器会调用视图解析器对view进行解析，添加前
        //templates/pages/goods.html
    }


}